home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / libgutil / bm.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  3KB  |  130 lines

  1. /*
  2.  * Copyright 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  *    Simple support for bitmap reading.
  19.  *
  20.  *
  21.  *    exports
  22.  *
  23.     void bmopen(name,mode,xsize,ysize)
  24.     void bmputrow(buf)
  25.     void bmgetrow(buf)
  26.     int bmrowbytes()
  27.     void bmsize(xsize,ysize)
  28.     void bmclose()
  29.  *
  30.  */
  31. #include "stdio.h"
  32. #include "bitmap.h"
  33.  
  34. static Bitmap b;
  35. static FILE *bmfile;
  36. static int bytesperrow;
  37.  
  38. #define BMMAGIC        0x5421
  39.  
  40. void bmopen(name,mode,xsize,ysize)
  41. char *name;
  42. char *mode;
  43. int xsize, ysize;
  44. {
  45.     long magic[2];
  46.  
  47.     if(mode[0] == 'r') {
  48.     bmfile = fopen(name,"r");
  49.     if(!bmfile) {
  50.         fprintf(stderr,"bmopen: can't open input file %s\n",name);
  51.         exit(1);
  52.     }
  53.     fread(magic,2*sizeof(long),1,bmfile);
  54.     if(magic[0] != BMMAGIC || magic[1] != BMMAGIC) {
  55.         fprintf(stderr,"bmopen: bad magic number in bit map header\n");
  56.         exit(1);
  57.     }
  58.     fread(&b.base,sizeof(short *),1,bmfile);
  59.     fread(&b.xsize,sizeof(short),1,bmfile);
  60.     fread(&b.ysize,sizeof(short),1,bmfile);
  61.     fread(&b.xorig,sizeof(short),1,bmfile);
  62.     fread(&b.yorig,sizeof(short),1,bmfile);
  63.     fread(&b.xmove,sizeof(short),1,bmfile);
  64.     fread(&b.ymove,sizeof(short),1,bmfile);
  65.     fread(&b.sper,sizeof(short),1,bmfile);
  66.     bytesperrow = 2*b.sper;
  67.     } else if(mode[0] == 'w') {
  68.     long magic[2];
  69.     Bitmap b;
  70.  
  71.     b.xsize = xsize;
  72.     b.ysize = ysize;
  73.     b.xorig = 0;
  74.     b.yorig = 0;
  75.     b.xmove = 0;
  76.     b.ymove = 0;
  77.     b.sper = 1+((xsize-1)>>4);
  78.     b.base = 0;
  79.     bmfile = fopen(name,"w");
  80.     if(!bmfile) {
  81.         fprintf(stderr,"bmopen: can't open output file %s\n",name);
  82.         exit(1);
  83.     }
  84.     magic[0] = BMMAGIC;
  85.     magic[1] = BMMAGIC;
  86.     fwrite(magic,2*sizeof(long),1,bmfile);
  87.     fwrite(&b.base,sizeof(short *),1,bmfile);
  88.     fwrite(&b.xsize,sizeof(short),1,bmfile);
  89.     fwrite(&b.ysize,sizeof(short),1,bmfile);
  90.     fwrite(&b.xorig,sizeof(short),1,bmfile);
  91.     fwrite(&b.yorig,sizeof(short),1,bmfile);
  92.     fwrite(&b.xmove,sizeof(short),1,bmfile);
  93.     fwrite(&b.ymove,sizeof(short),1,bmfile);
  94.     fwrite(&b.sper,sizeof(short),1,bmfile);
  95.     bytesperrow = 2*b.sper;
  96.     } else {
  97.     fprintf(stderr,"bmopen: bad mode %s\n",mode);
  98.     exit(1);
  99.     }
  100. }
  101.  
  102. void bmputrow(buf)
  103. char *buf;
  104. {
  105.     fwrite(buf,bytesperrow,1,bmfile);
  106. }
  107.  
  108. void bmgetrow(buf)
  109. char *buf;
  110. {
  111.     fread(buf,bytesperrow,1,bmfile);
  112. }
  113.  
  114. int bmrowbytes()
  115. {
  116.     return bytesperrow;
  117. }
  118.  
  119. void bmsize(xsize,ysize)
  120. int *xsize, *ysize;
  121. {
  122.     *xsize = b.xsize;
  123.     *ysize = b.ysize;
  124. }
  125.  
  126. void bmclose()
  127. {
  128.     fclose(bmfile);
  129. }
  130.